package com.ruoyi.washer.service.impl;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.washer.mapper.XwEnduserTeamMapper;
import com.ruoyi.washer.domain.XwEnduserTeam;
import com.ruoyi.washer.service.IXwEnduserTeamService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * 终端团队Service业务层处理
 *
 * @author hawking
 * @date 2023-12-26
 */
@Service
public class XwEnduserTeamServiceImpl implements IXwEnduserTeamService
{
    @Autowired
    private XwEnduserTeamMapper xwEnduserTeamMapper;

    /**
     * 查询终端团队
     *
     * @param teamId 终端团队主键
     * @return 终端团队
     */
    @Override
    public XwEnduserTeam selectXwEnduserTeamByTeamId(Long teamId)
    {
        return xwEnduserTeamMapper.selectXwEnduserTeamByTeamId(teamId);
    }

    /**
     * 查询终端团队列表
     *
     * @param xwEnduserTeam 终端团队
     * @return 终端团队
     */
    @Override
    public List<XwEnduserTeam> selectXwEnduserTeamList(XwEnduserTeam xwEnduserTeam)
    {
        return xwEnduserTeamMapper.selectXwEnduserTeamList(xwEnduserTeam);
    }

    /**
     * 新增终端团队
     *
     * @param xwEnduserTeam 终端团队
     * @return 结果
     */
    @Override
    public int insertXwEnduserTeam(XwEnduserTeam xwEnduserTeam)
    {
        return xwEnduserTeamMapper.insertXwEnduserTeam(xwEnduserTeam);
    }

    /**
     * 修改终端团队
     *
     * @param xwEnduserTeam 终端团队
     * @return 结果
     */
    @Override
    public int updateXwEnduserTeam(XwEnduserTeam xwEnduserTeam)
    {
        return xwEnduserTeamMapper.updateXwEnduserTeam(xwEnduserTeam);
    }

    /**
     * 批量删除终端团队
     *
     * @param teamIds 需要删除的终端团队主键
     * @return 结果
     */
    @Override
    public int deleteXwEnduserTeamByTeamIds(Long[] teamIds)
    {
        return xwEnduserTeamMapper.deleteXwEnduserTeamByTeamIds(teamIds);
    }

    /**
     * 删除终端团队信息
     *
     * @param teamId 终端团队主键
     * @return 结果
     */
    @Override
    public int deleteXwEnduserTeamByTeamId(Long teamId)
    {
        return xwEnduserTeamMapper.deleteXwEnduserTeamByTeamId(teamId);
    }
    /*

     */
    public List<XwEnduserTeam> getEnduserReplenishmanList(String openId){
        XwEnduserTeam xwEnduserTeam=new XwEnduserTeam();
        xwEnduserTeam.setAdminOpenId(openId);
        xwEnduserTeam.setDeleteFlag("N");
        return xwEnduserTeamMapper.selectXwEnduserTeamList(xwEnduserTeam);
    }
    @Transactional
    public JSONObject createEnduserInvitationCode(String openId){
        JSONObject jsonObject=new JSONObject();
        XwEnduserTeam xwEnduserTeam=new XwEnduserTeam();
        xwEnduserTeam.setAdminOpenId(openId);
        xwEnduserTeam.setDeleteFlag("N");
        xwEnduserTeamMapper.insertXwEnduserTeam(xwEnduserTeam);
        String teamId=""+ xwEnduserTeam.getTeamId();
        xwEnduserTeam.setInvitationCode(teamId);
        xwEnduserTeam.setInvitationTime(new Date());
        xwEnduserTeamMapper.updateXwEnduserTeam(xwEnduserTeam);
        jsonObject.put("invitationCode",teamId);
        return jsonObject;
    }
    /*
    * */
    public int deleteEnduserReplenishmanByTeamId(String teamId){
        XwEnduserTeam xwEnduserTeam=new XwEnduserTeam();
        xwEnduserTeam.setTeamId(Long.parseLong(teamId));
        xwEnduserTeam.setDeleteFlag("Y");
        xwEnduserTeam.setUpdatedTime(new Date());
        return xwEnduserTeamMapper.updateXwEnduserTeam(xwEnduserTeam);
    }

    public List<XwEnduserTeam> getJoinReplenishmanList(String openId){
        XwEnduserTeam xwEnduserTeam=new XwEnduserTeam();
        xwEnduserTeam.setReplenishmanOpenId(openId);
        xwEnduserTeam.setDeleteFlag("N");
        return xwEnduserTeamMapper.selectXwEnduserTeamList(xwEnduserTeam);
    }

    public int bingReplenishmanByInvitationcode(String openId,
                                                String invitationCode){
        XwEnduserTeam xwEnduserTeam=new XwEnduserTeam();
        xwEnduserTeam.setAdminOpenId(openId);
        xwEnduserTeam.setInvitationCode(invitationCode);
        XwEnduserTeam xwEnduserTeam1=xwEnduserTeamMapper.selectXwEnduserTeamByInvitationCode(xwEnduserTeam);
        if(xwEnduserTeam1==null){
            return 1;
        }else{
            XwEnduserTeam xwEnduserTeam2=new XwEnduserTeam();
            xwEnduserTeam2.setAdminOpenId(xwEnduserTeam1.getAdminOpenId());
            xwEnduserTeam2.setReplenishmanOpenId(openId);
            XwEnduserTeam xwEnduserTeam3=xwEnduserTeamMapper.checkExistTeam(xwEnduserTeam2);
            if(xwEnduserTeam3==null){
                xwEnduserTeam1.setReplenishmanOpenId(openId);
                xwEnduserTeam1.setUpdatedTime(new Date());
                xwEnduserTeamMapper.updateXwEnduserTeam(xwEnduserTeam1);
                return 2;
            }else{
                return 1;
            }
        }

    }
}
